<template>
  <div class="auth-page">
    <div class="auth-container">
      <div class="auth-form">
        <div class="header">
          <img src="@/assets/logo.webp" alt="logo" class="logo">
          <h2>欢迎登录</h2>
          <p class="sub-title">餐饮预订系统</p>
        </div>

        <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          label-position="top"
        >
          <el-form-item prop="username" label="用户名">
            <el-input
              v-model="form.username"
              prefix-icon="User"
              placeholder="请输入用户名"
            />
          </el-form-item>

          <el-form-item prop="password" label="密码">
            <el-input
              v-model="form.password"
              prefix-icon="Lock"
              type="password"
              show-password
              placeholder="请输入密码"
              @keyup.enter="handleSubmit"
            />
          </el-form-item>

<!--          <div class="form-footer">-->
<!--            <el-checkbox v-model="rememberMe">记住我</el-checkbox>-->
<!--            <el-button type="text">忘记密码？</el-button>-->
<!--          </div>-->

          <el-button
            type="primary"
            class="submit-btn"
            :loading="loading"
            @click="handleSubmit"
          >
            登录
          </el-button>

          <div class="register-link">
            还没有账号？
            <router-link to="/register">立即注册</router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const formRef = ref(null)
const loading = ref(false)
const rememberMe = ref(false)

const form = ref({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleSubmit = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    loading.value = true

    await userStore.login(form.value)
    router.push('/')
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.response?.data?.msg || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.auth-container {
  width: 100%;
  max-width: 400px;
  margin: 0 20px;
}

.auth-form {
  background: #fff;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.1);
}

.header {
  text-align: center;
  margin-bottom: 40px;

  .logo {
    width: 80px;
    margin-bottom: 20px;
  }

  h2 {
    margin: 0 0 10px;
    font-size: 24px;
    color: #333;
  }

  .sub-title {
    margin: 0;
    color: #666;
    font-size: 16px;
  }
}

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  font-size: 16px;
}

.register-link {
  margin-top: 16px;
  text-align: center;
  color: #666;

  a {
    color: var(--el-color-primary);
    text-decoration: none;

    &:hover {
      text-decoration: underline;
    }
  }
}
</style>
